Global variable

In computer programming, a global variable is a variable that is accessible in every scope (unless shadowed). Interaction mechanisms with global variables are called global environment (see also global state) mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory (and therefore all interactions can be reconducted to message passing).

They are usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere (unless they reside in protected memory or are otherwise rendered read-only), and any part of the program may depend on it.[1] A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions. Global variables also make it difficult to integrate modules because software written by others may use the same global names unless names are reserved by agreement, or by naming convention.

Global variables are used extensively to pass information between sections of code that do not share a caller/callee relation like concurrent threads and signal handlers. Languages (including C) where each file defines an implicit namespace eliminate most of the problems seen with languages with a global namespace though some problems may persist without proper encapsulation. Without proper locking (such as with a mutex), code using global variables will not be thread-safe except for read only values in protected memory.

Contents

C and C++

The C language does not have a global keyword. However, variables declared outside a function implicitly have a scope covering everything in the .c file or compilation unit containing its declaration. In a small program contained in a single file, such variables effectively have global scope. On the other hand, a variable that is required to have global-scope in a multi-file project needs to be imported individually into each file using the extern keyword. Such global access can be made implicit by placing the extern declaration in a shared header file, since it is common practice for all .c files in a project to include at least one .h file: the standard header file errno.h is an example, making the errno variable accessible to all modules in a project. Where this global access mechanism is judged problematic, it can be disabled using the static keyword which restricts a variable to file scope, and will cause attempts to import it with extern to raise a compilation error.[2]

An example of a "global" variable in C:

/* Note that this example is correct.
     global does not qualify as a global variable as it is not "in scope everywhere".
 */
#include <stdio.h>
 
int myGlobal = 3; /* This is the external variable. */
 
static void ChangeMyGlobal(void)
{
   myGlobal = 5; /* Reference to external variable in a function. */
}
 
int main(void)
{
   printf("%d\n", myGlobal); /* Reference to external variable in another function. */
   ChangeMyGlobal();
   printf("%d\n", myGlobal);
   return 0;
}

As the variable is an external one, there is no need to pass it as a parameter to use it in a function besides main. It belongs to every function in the module.

The output will be:

3
5

The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program. They make separating code into reusable libraries more difficult because many systems (such as DLLs) don't directly support viewing global variables in other modules. They can lead to problems of naming because a global variable makes a name dangerous to use for any other local or object scope variable. A local variable of the same name can shield the global variable from access, again leading to harder to understand code. The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code.

Environment variables

Environment variables are a facility provided by some operating systems. Within the OS's shell (ksh in Unix, bash in Linux, command.exe in DOS and cmd.exe in Windows) they are a kind of variable: for instance, in unix and related systems an ordinary variable becomes an environment variable when the export keyword is used. Program code other than shells has to access them by API calls, such as getenv() and setenv().

They are local to the process in which they were set. That means if we open two terminal windows (Two different processes running shell) and change value of environment variable in one window, that change will not be seen by other window.

When a child process is created, it inherits all the environment variables and their values from the parent process. Usually, when a program calls another program, it first creates a child process by forking, then the child adjusts the environment as needed and lastly the child replaces itself with the program to be called. Child processes therefore cannot use environment variables to communicate with their peers, avoiding the action at a distance problem.

Java: no explicit globals

Some languages, like Java, don't have global variables. In Java, all variables that are not local variables are fields of a class. Hence all variables are in the scope of either a class or a method. In Java, static fields (aka class variables) exist independently of any instances of the class and one copy is shared among all instances; hence static fields are used for many of the same purposes as global variables in other languages because of their similar "sharing" behavior.

PHP: globals and superglobals

PHP has a global keyword and a number of unusual ways of using global variables. Variables declared outside functions have file scope (which is for most purposes the widest scope). However, they are not accessible inside functions unless imported with the global keyword (i.e., the keyword accesses global variables, it does not declare) them. However, some predefined variables, known as superglobals are always accessible. They are all arrays. A general purpose one is the $GLOBALS superglobal, which contains all the variables defined out of function scope. Changes to its elements change the original variables, and additions create new variables. The superglobals $_POST and $_GET are widely used in web programming.

Global-only and global-by-default

A number of non-structured languages, such as (early versions of) BASIC, COBOL and Fortran I only provide global variables. Fortran II introduced subroutines with local variables and COMMON for global variables. Named COMMON groups globals somewhat like namespaces. Variables are global by default in FORTH, Lua and most command shells.

Other languages

See also

References

  1. ^ William Wulf and Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28–34.
  2. ^ C in a Nutshell, P.Prinz & T Crawford, 2006, O'Reilly, Ch 11